﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace System.Collections.Generic
{
	public static class TListExtension
	{
		/// <summary>
		/// Removes all items from list where condition "where" applies
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="list"></param>
		/// <param name="where"></param>
		public static void RemoveWhere<T>(this List<T> list, Func<T,bool> where)
		{
			for (int i= list.Count-1; i>= 0; i--)
			{
				var elem = list[i];
				if (where(elem))
					list.RemoveAt(i);
			}
		}

		/// <summary>
		/// Clears the list and sets the new items.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="list"></param>
		/// <param name="items"></param>
		public static void SetRange<T>(this List<T> list, IEnumerable<T> items)
		{
			list.Clear();
			list.AddRange(items);
		}

		/// <summary>
		/// True, if enumerations have same size and elements are equal (object.Equals() - method).
		/// Parameters @this and enumeration may be null.
		/// </summary>
		/// <typeparam name="T"></typeparam>
		/// <param name="this"></param>
		/// <param name="enumeration"></param>
		/// <returns></returns>
		public static bool AreEquals<T>(this IEnumerable<T> @this, IEnumerable<T> enumeration)
		{
			if ( @this == null )
				return enumeration == null;

			if (enumeration == null)
				return false;

			int count = @this.Count();
			if ( count != enumeration.Count() )
				return false;

			var thisIdx = @this.GetEnumerator();
			var otherIdx = enumeration.GetEnumerator();

			for (int i = 0; i < count; i++)
			{
				thisIdx.MoveNext();
				otherIdx.MoveNext();
				if (!thisIdx.Current.Equals(otherIdx.Current))
					return false;
			}

			return true;
		}

	}
}
